<template>
  <el-card class="good-container">
    <template #header>
      <div class="header">
        <el-input
          style="width: 200px; margin-right: 10px"
          placeholder="请输入关键字"
          v-model="keyword"
          @change="handleOption"
          size="small"
          clearable
        />
        <!-- <el-button type="danger" size="small" icon="el-icon-delete" @click="handleClose()">批量删除</el-button> -->
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新增角色</el-button>
      </div>
    </template>
    <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      :tree-props="{children: 'permissionList'}">
      >

      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column
        prop="id"
        label="编号"
        width="50"
      >
      </el-table-column>

      <el-table-column
        prop="name"
        label="名称"
      >
      </el-table-column>

      <el-table-column
        prop="description"
        label="简介"
      >
      </el-table-column>

      <el-table-column
        label="权限列表"
      >
        <template #default="scope">
          <blockquote v-for="permission in scope.row.permissionList" :key="permission">
            {{permission.name}}
          </blockquote>
        </template> 
      </el-table-column>

      <el-table-column
        prop="createTime"
        label="创建时间"
      >
      </el-table-column>

      <el-table-column
        prop="updateTime"
        label="更新时间"
      >
      </el-table-column>

      <el-table-column
        label="操作"
        width="120"
      >
        <template #default="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="changePage"
    />
  </el-card>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import axios from '@/utils/axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import api from '@/api/api'
export default {
  name: 'Role',
  setup() {
    const multipleTable = ref(null)
    const router = useRouter()
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      keyword: '', // 搜索商品名参数
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10 // 分页大小
    })
    onMounted(() => {
      getRoleList()
    })
    // 获取轮播图列表
    const getRoleList = () => {
      let pageInfo = {
        pageNumber: state.currentPage,
        pageSize: state.pageSize,
        keyword: state.keyword
      }
      state.loading = true
      axios.post(api.role.list, pageInfo).then(res => {
        let pageInfo = res.data.data
        state.tableData = pageInfo.data
        state.total = pageInfo.total
        state.currentPage = pageInfo.pageNumber
        state.loading = false
      })
    }
    const handleAdd = () => {
      router.push({ path: '/roleSaveOrUpdate' })
    }
    const handleEdit = (role) => {
      router.push({ path: '/roleSaveOrUpdate', query: role  })
    }
    const handleDelete = (id) => {
      ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        .then(() => {
          console.log("come in sss")
          axios.get(api.role.delete+'?id='+id).then((res) => {
            console.log(res.data);
            if(res.data.code !== '00000') {
              ElMessage({
                type: 'info', 
                message: '删除失败'
                })
            }else{
              ElMessage({
              type: 'success',
              message: '删除成功!',
              });
              getRoleList();
            }
          })
         }) 
    }
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
    const handleOption = () => {
      getRoleList()
    }
    const changePage = (val) => {
      state.currentPage = val
      getRoleList()
    }
    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      handleAdd,
      handleOption,
      handleEdit,
      getRoleList,
      changePage,
      handleDelete,
    }
  }
}
</script>

<style scoped>
  .good-container {
    min-height: 100%;
  }
  .el-card.is-always-shadow {
    min-height: 100%!important;
  }
</style>